;
(function (window, angular) {
    'use strict';
    angular.module("Controller.Report.PlanDealerAccuracy", [])
        .controller("PlanDealerAccuracyCtrl", ["$scope", "$state", "ybWidget", "ybHttp", "MESG_TYPE", "$filter", "$timeout", "UserContextService", function ($scope, $state, ybWidget, ybHttp, MESG_TYPE, $filter, $timeout, UserContextService) {
            $scope.markInfo = {
                curRow: -1
            };
            $scope.pageInfo = {
                totalPages: 1,
                totalRows: 0,
                pageSize: 10,
                curPage: 1
            };
            $scope.toolbarConfig = {
                showColConfig: false
            };
            $scope.datePickerFlag = [false, false];
            // 所有月份信息
            $scope.monthData = [
                {name: '1月', key: 'jan', value: 1, isChecked: true, isSearch: true}, {name: '2月', key: 'feb', value: 2, isChecked: true, isSearch: true},
                {name: '3月', key: 'mar', value: 3, isChecked: true, isSearch: true}, {name: '4月', key: 'apr', value: 4, isChecked: true, isSearch: true},
                {name: '5月', key: 'may', value: 5, isChecked: true, isSearch: true}, {name: '6月', key: 'jun', value: 6, isChecked: true, isSearch: true},
                {name: '7月', key: 'jul', value: 7, isChecked: true, isSearch: true}, {name: '8月', key: 'aug', value: 8, isChecked: true, isSearch: true},
                {name: '9月', key: 'sept', value: 9, isChecked: true, isSearch: true}, {name: '10月', key: 'oct',value: 10, isChecked: true, isSearch: true},
                {name: '11月', key: 'nov', value: 11, isChecked: true, isSearch: true}, {name: '12月', key: 'dec', value: 12, isChecked: true, isSearch: true}
            ];
            // 时间选择器配置
            $scope.dateOptions = {
                minMode:'year',
                datepickerMode: 'year'
            };
            $scope.condition = {};
            $scope.dataList = [];
            $scope.tbConf = {
                scrollWidth: 1200,
                tableCol: [
                    {
                        type: "",
                        name: "经销商名称",
                        dataField: "dealerOrgName",
                        width: 50,
                        isShow: true,
                        isFixed: false,
                        toSort: false,
                        sortType: ""
                    },
                    {
                        type: "",
                        name: "零件编号",
                        dataField: "materialsNo",
                        width: 50,
                        isShow: true,
                        isFixed: false,
                        toSort: false,
                        sortType: ""
                    },
                    {
                        type: "",
                        name: "零件中文名",
                        dataField: "materialsDes",
                        width: 50,
                        isShow: true,
                        isFixed: false,
                        toSort: false,
                        sortType: ""
                    },
                    {
                        type: "",
                        name: "数量汇总值",
                        dataField: "purchaseNumSum",
                        width: 50,
                        isShow: true,
                        isFixed: false,
                        toSort: false,
                        sortType: ""
                    },
                    {
                        type: "",
                        name: "计划数量",
                        dataField: "planNumSum",
                        width: 50,
                        isShow: true,
                        isFixed: false,
                        toSort: false,
                        sortType: ""
                    },
                    {
                        type: "",
                        name: "计划准确率",
                        dataField: "planAccuracy",
                        width: 50,
                        isShow: true,
                        isFixed: false,
                        toSort: false,
                        sortType: ""
                    },
                    {
                        type: "",
                        name: "月份",
                        dataField: "monthly",
                        width: 50,
                        isShow: true,
                        isFixed: false,
                        toSort: false,
                        sortType: ""
                    }
                ]
            };

            $scope.switchDataPicker = function (index) {
                $scope.datePickerFlag = [false, false];
                if (angular.isDefined(index)) {
                    $scope.datePickerFlag[index] = true;
                }
            };
            // 选择月份筛选条件
            $scope.selectMonth = function () {
                var checkedMonth = [];          // 已选中的月份
                var monthCount = [];            // 调接口筛选的参数
                angular.forEach($scope.monthData, function (item) {
                    if (item.isChecked) {
                        checkedMonth.push(item.name);
                        monthCount.push(item.value);
                    }
                });
                $scope.condition.monthCount = monthCount;
                $scope.condition.months = checkedMonth.join(',');
            };
            $scope.exportParam = {
                allUrl: ybHttp.RequestUrl("dReport", "/buyPlanDealer/reportExportAll")
            };

            $scope.reset = function () {
                $scope.condition = {};
            };

            var btnPermissionCode = {
                export: window.ActionCode.planDealerAccuracyExport
            };

            $scope.btnPermission = {
                export: true
            };

            function setBtnPermission() {
                for (var key in btnPermissionCode) {
                    $scope.btnPermission[key] = UserContextService.CheckPermission([btnPermissionCode[key]]);
                }
            }

            $scope.checkDate = function (obj, startFiled, endFiled) {
                var timeFormatObj = ybWidget.CheckDate(obj[startFiled], obj[endFiled]);
                if (!!timeFormatObj.tStart) {
                    obj[startFiled] = timeFormatObj.tStart;
                }
                if (!!timeFormatObj.tEnd) {
                    obj[endFiled] = timeFormatObj.tEnd;
                }
            };

            function getDataList(curPage) {
                $scope.markInfo.curRow = -1;
                var service = "dReport";
                var reqUrl = "/buyPlanDealer/reportList";
                var param = {};
                param.pageSize = $scope.pageInfo.pageSize;
                if (!!curPage) {
                    $scope.pageInfo.curPage = curPage;
                }
                if ($scope.condition.months) {
                    if (!$scope.condition.year) {
                        ybWidget.ShowMessage('年份不能为空，请选择年份！', MESG_TYPE.Error);
                        return;
                    } 
                }

                param.pageNum = $scope.pageInfo.curPage;
                param.filters = angular.copy($scope.condition);
                ybHttp.Post(service, reqUrl, param).then(function (resp) {
                    if (resp.code !== 0) {
                        ybWidget.ShowMessage(resp, MESG_TYPE.Error);
                        return;
                    }
                    angular.forEach(resp.value.list, function (item) {
                    });
                    $scope.pageInfo.totalRows = resp.value.total;
                    $scope.pageInfo.totalPages = resp.value.pages;
                    $scope.dataList = resp.value.list;
                });
            }

            $scope.toSort = function (config, index) {
                ybWidget.ToSort(config, index);
            };

            $scope.switchToolbar = function (toolbar) {
                if (!!$scope.toolbarConfig["show" + toolbar]) {
                    $scope.toolbarConfig["show" + toolbar] = false;
                } else {
                    $timeout(function () {
                        $scope.toolbarConfig["show" + toolbar] = true;
                    });
                }
            };

            $scope.selectRow = function (index) {
                $scope.markInfo.cueRow = index
            };

            $scope.search = function (curpage) {
                getDataList(curpage);
            };

            (function () {
                //getDataList();
                UserContextService.SetBtnPermission(setBtnPermission);
            })();
        }])
    ;
})(window, window.angular);